package org.example.auth.core.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.auth.core.entity.Account;
import org.example.auth.core.mapper.AccountMapper;
import org.example.auth.core.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.auth.form.PwdLoginForm;
import org.example.system.common.utils.JwtUtils;
import org.example.system.common.utils.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 账号信息表 服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-05-05
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {
    @Resource
    AccountMapper accountMapper;

    @Override
    public Account queryByName(String accountNum) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("accountNum",accountNum);
        return accountMapper.selectOne(qw);
    }

    @Override
    public ResponseUtil pwdLogin(PwdLoginForm form) {
        Account account = this.queryByName(form.getAccountNum());
        //校验密码
        if(account==null||!SecureUtil.md5(form.getAccountPwd().trim()).equals(account.getAccountPwd())){
            return ResponseUtil.noAuthority5();
        }
        //生成token
        String token = JwtUtils.generateToken(account.getId());
        return ResponseUtil.success(token);
    }
}
